﻿using Dt261.help;
using Dt261.jsonmodel;
using Dt261.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dt261
{
    class GlobalData
    {
        public GlobalData()
        {
            ReadConfigInfo();
        }
        //中文输入不了
        private static User loginUser;

        private static string titleText;

        public static User LoginUser { get => loginUser; set => loginUser = value; }

        public static bool IsSuperUser()
        {
            return LoginUser.Name.Equals("超级用户");
        }

        public static string HospitalTitleText
        {
            get => titleText; set
            {
                titleText = value; SaveonfigInfo();
                DMSkin.Core.Broadcast.PushBroadcast<string>("UpdateTitle");
            }
        }

        private static UserInfo loginNetUser;

        public static UserInfo LoginNetUser { get => loginNetUser; set => loginNetUser = value;  }


        private static string host;
        private static bool isDeleteLocal;
        private static int imageType;
    

        public static string Host { get => host; set { host = value; SaveonfigInfo(); } }
        public static bool IsDeleteLocal { get => isDeleteLocal; set { isDeleteLocal = value; SaveonfigInfo(); } }

        public static int ImageType { get => imageType; set { imageType = value; SaveonfigInfo(); }
}

        public static void ReadConfigInfo()
        {
            var basePath = Environment.CurrentDirectory;

            string cfgINI = System.IO.Path.Combine(basePath, "host.ini");

            try
            {
                if (File.Exists(cfgINI))
                {
                    IniFile ini = new IniFile(cfgINI);
                    host = ini.IniReadValue("Config", "Host");
                    //  Password = ini.IniReadValue("Login", "Password");
                    isDeleteLocal = ini.IniReadValue("Config", "IsDeleteLocal") == "Y";

                    imageType = int.Parse(ini.IniReadValue("Config", "HearingImage")) ;

                    HospitalTitleText = ini.IniReadValue("Config", "HospitalTitleText");
                }
            }
            catch
            {
               
            }

        }

        /// <summary>
        /// 保存登录信息
        /// </summary>
        private static void SaveonfigInfo()
        {
            var basePath = Environment.CurrentDirectory;

            string cfgINI = System.IO.Path.Combine(basePath, "host.ini");
            IniFile ini = new IniFile(cfgINI);
            ini.IniWriteValue("Config", "Host", Host);
            ini.IniWriteValue("Config", "IsDeleteLocal", IsDeleteLocal ? "Y" : "N");
            ini.IniWriteValue("Config", "HearingImage", ImageType.ToString());
            ini.IniWriteValue("Config", "HospitalTitleText", HospitalTitleText);
        }

    }
}
